<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <link rel="icon" href="">
  <title>测试网页</title>
  <script type="text/javascript">
    function getLanguage() {
      return navigator.language || navigator.browserLanguage;
    }

    function supportRecord(){
      var AC = window.AudioContext || window.webkitAudioContext;
      if(!AC) return 'No AudioContext';
      var scope = navigator.mediaDevices || { };
      if(!scope.getUserMedia){
        scope = navigator;
        scope.getUserMedia || (scope.getUserMedia = scope.webkitGetUserMedia || scope.mozGetUserMedia || scope.msGetUserMedia);
      };
      if(!scope.getUserMedia) return 'No getUserMedia';
      return 'Yes';
    }

    async function testWasmSupport() {
      const result = {
        wasmSupported: false,
        streamingSupported: false,
        reason: "",
        details: ""
      };

      // 基本支持检测
      if (typeof WebAssembly !== "object") {
        result.reason = "WebAssembly 对象不存在";
        return result;
      }

      if (typeof WebAssembly.instantiate !== "function") {
        result.reason = "WebAssembly.instantiate 不可用";
        return result;
      }

    // 检查是否能 Validate 一个最小模块
      try {
        const minimalModule = new Uint8Array([0x00,0x61,0x73,0x6d,0x01,0x00,0x00,0x00]); 
        const valid = WebAssembly.validate(minimalModule);
        if (!valid) {
          result.reason = "WebAssembly.validate 返回 false — 可能被禁用";
          return result;
        }
        result.wasmSupported = true;
      } catch (e) {
        result.reason = "无法 validate 最小 WASM 模块";
        result.details = e.stack || e.toString();
        return result;
      }

    // 检测 streaming 支持
      if (typeof WebAssembly.instantiateStreaming !== "function") {
        result.streamingSupported = false;
      } else {
        try {
        // 模拟一个假的 response（不实际请求）
          const fakeResponse = new Response(
            new Uint8Array([0x00,0x61,0x73,0x6d,0x01,0x00,0x00,0x00]),
            { headers: { "Content-Type": "application/wasm" } }
        );
          await WebAssembly.instantiateStreaming(Promise.resolve(fakeResponse));
          result.streamingSupported = true;
        } catch (e) {
          result.streamingSupported = false;
          result.details = "instantiateStreaming 不可用：" + (e.message || e);
        }
      }
      return result;
    }

    function onload(){
      document.getElementById('lang').innerText = `当前语言: ${getLanguage()}`;
      document.getElementById('record').innerText = `支持录音: ${supportRecord()}`;

      testWasmSupport().then(res => {
        console.log("WASM 检测结果：", res);
        document.getElementById('wasm').innerText = `wasmSupported: ${res.wasmSupported}; streamingSupported: ${res.streamingSupported}; reason: ${res.reason}; details: ${res.details}`;
      });
    }

  </script>
</head>

<body onload="onload()" style="padding: 20px; font-size: 30px;">
  <div id="lang"></div>
  <div id="record"></div>
  <div id="wasm"></div>
</body>

</html>